#####################################################################
# Product:      KnowledgeEngine (Plone Edition)
#
# File:         Sortable.py
#
# Copyright:    Copyright (c) 2007, Faulkner Technologies
#
# Author:       James Davies
#
# Description:  Sortable is a Mixin class used to allow Models and 
#               Repositories to sort assessments within the MyKnowledge area
#
#####################################################################

from Products.KnowledgeEngine.PropertyDefinition import PropertyDefinition
from Products.KnowledgeEngine.PermissionDefinition import *
from Products.KnowledgeEngine.PropertyDefinition import *
from Products.KnowledgeEngine.PropertyTypes import *
from Products.KnowledgeEngine.PropertyFormats import *
from Products.KnowledgeEngine.PropertyStructures import *

ASSESSMENT_SORT_OPTIONS = [ ('None',                None),
                            ('Assessment Title',  'name',), 
                            ('Creation Time',     'instantCreated',  ),
                            ('Last Modified',     'instantModified'),
                            ('Created By',        'createdBy')]


class SortableInfo:
    """ Mixin Class to provide sortable field info """
    
    def onConfigureProperties(self):
    
        # Allow setting of sort order in the myknowledege area
        self.addPropertyDefinition( PropertyDefinition( 
            identity        = "assessmentSortOrder",
            displayName     = "Assessment Sort Order",
            description     = "Field to sort assessments on in MyKnowledge area",
            defaultValue    = "",
            propertyType    = pt_STRING,
            propertyFormat  = pf_NATIVE,
            hasEnumeratedValues = True, 
            enumeratedValuesMapping = ASSESSMENT_SORT_OPTIONS
        ))
        
        # Allow reverse sorts
        self.addPropertyDefinition(PropertyDefinition(
            identity       = "assessmentSortReverse", 
            displayName    = "Reverse Sort",
            description    = "Reverse the order of assessment sorting",
            defaultValue   = False,
            propertyType   = pt_BOOLEAN,
            propertyFormat = pf_CHECKBOX
        ))    


class Sortable:
    """ Mixin class to provie sortable field info """
    
    
    def getAssessmentSortOrder(self):
        """ Returns the sort order for this model """
        
        return getattr(self, 'assessmentSortOrder', None)
    
    #---------------------------------------------------------------#

    def setAssessmentSortOrder(self, assessmentSortOrder):
        """ Returns the sort order for this model """
        
        assert assessmentSortOrder in dict(ASSESSMENT_SORT_OPTIONS).keys()
        
        setattr(self, 'assessmentSortOrder', assessmentSortOrder)
    
    #---------------------------------------------------------------#
    
    
    def getAssessmentSortReverse(self):
        """ Returns the sort order for this model """
        
        return getattr(self, 'assessmentSortReverse', False)
    
    #---------------------------------------------------------------#    
    
    def setAssessmentSortReverse(self, assessmentSortReverse):
        """ Returns the sort order for this model """
        
        if assessmentSortReverse: assessmentSortReverse = True
        else: assessmentSortReverse = False
        
        setattr(self, 'assessmentSortReverse', assessmentSortReverse)
    
    #---------------------------------------------------------------#        
    
    
    
    
    
    